<template>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :append-to-body="true"
        :close-on-click-modal="false">
        <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px"
            style="width: 80%; margin-left:50px;">
            <el-row :gutter="40">
                <el-col :span="24">
                    <el-form-item label="目录名称" prop="name">
                        <el-input v-model="temp.name" />
                    </el-form-item>
                </el-col>

            </el-row>
            <el-row :gutter="40">
                <el-col :span="24">
                    <el-form-item label="目录描述" prop="description">
                        <el-input v-model="temp.description" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" v-if="dialogStatus!='look'">
            <el-button @click="dialogFormVisible = false">
                {{ $t('table.cancel') }}
            </el-button>
            <el-button type="primary" :loading="loading" @click="dialogStatus === 'create' ? createData() : updateData()">
                {{ $t('table.confirm') }}
            </el-button>
        </div>
        <!-- <div class="el-message el-message--error"><p>请填写完整</p></div> -->
    </el-dialog>
</template>

<script>
import { fetchMasterdataDirectoryList, createMasterdataDirectory, updateMasterdataDirectory, getMasterdataDirectory, deleteMasterdataDirectory } from '@/api/masterdata/masterdataDirectory'


export default {
    name: 'treeData',
    data() {
        return {
            temp: {
                name: undefined,
                desc: undefined
            },
            textMap: {
                update: '编辑',
                create: '新建',
                look:'查看'
            },
            rules: {},
            loading: false,
            dialogFormVisible: false,
            dialogStatus: '',
        }
    },
    methods: {
        setList(data) {
        },
        handleCreate(data,type) {
            this.dialogStatus = 'create'
            this.temp = data
            this.temp.ancestralId=type
            this.dialogFormVisible = true
        },
        createData() {
            this.dialogFormVisible = false
            createMasterdataDirectory(this.temp).then(res => {
                if (res.data.code == 0) {
                    this.$message.success(res.data.msg)
                    this.$emit('getTreeList')
                } else {
                    this.$message.error(res.data.msg)
                }
            })
        },
        handleUpdate(data,type) {
            this.dialogStatus = 'update'
            this.dialogFormVisible = true
            this.temp = data
            this.temp.ancestralId=type
        },
        handleLook(data){
            this.dialogStatus = 'look'
            this.dialogFormVisible = true
            this.temp = data
        },
        updateData() {
            this.dialogFormVisible = false
            updateMasterdataDirectory(this.temp).then(res => {
                if (res.data.code == 0) {
                    this.$message.success(res.data.msg)
                    this.$emit('getTreeList')
                } else {
                    this.$message.error(res.data.msg)
                }
            })
        }
    }
}
</script>
<style>
.el-dialog {
    width: 40%;
}
</style>

